<script lang="ts" setup>
import { inject } from "vue";
export interface Product {
  id: number;
  name: string;
  describe: string;
  price: number;
  isHot?: boolean;
  quantity?: number;
}
const emit = defineEmits(["childremoveItem"]);
const cart = inject("cart") as {
  items: Product[];
  removeItem: (productId: number) => void;
  total: number;
};
const removeItem = (productId: number) => {
  cart.removeItem(productId);
};
</script>
<template>
  <div class="cart-container">
    <div class="cart-header">
      <h2>购物车</h2>
      <span class="item-count">{{ cart.items.length }}件商品</span>
    </div>
    <div class="cart-items">
      <div v-if="cart.items.length === 0" class="empty-cart">购物车是空的</div>
      <div v-else v-for="item in cart.items" :key="item.id" class="cart-item">
        <div class="item-info">
          <h4>{{ item.name }}</h4>
          <p class="item-desc">{{ item.describe }}</p>
          <p class="item-price">¥{{ item.price.toFixed(2) }}</p>
        </div>
        <div class="item-actions">
          <span class="quantity">x{{ item.quantity || 1 }}</span>
          <button @click="removeItem(item.id)" class="remove-btn">删除</button>
        </div>
      </div>
    </div>
    <div v-if="cart.items.length > 0" class="cart-footer">
      <div class="total">
        <span>总计:</span>
        <span class="total-price">¥{{ cart.total.toFixed(2) }}</span>
      </div>
      <button class="checkout-btn">去结算</button>
    </div>
  </div>
</template>
<style scoped>
.cart-container {
  position: fixed;
  right: 20px;
  top: 80px;
  width: 300px;
  background-color: white;
  border-radius: 8px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
  z-index: 100;
}
.cart-header {
  padding: 15px;
  border-bottom: 1px solid #e0e0e0;
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.item-count {
  font-size: 14px;
  color: #666;
}
.cart-items {
  max-height: 400px;
  overflow-y: auto;
  padding: 10px;
}
.empty-cart {
  padding: 20px;
  text-align: center;
  color: #666;
}
.cart-item {
  display: flex;
  justify-content: space-between;
  padding: 10px;
  border-bottom: 1px solid #f0f0f0;
}
.item-info {
  flex: 1;
}
.item-info h4 {
  margin: 0 0 5px 0;
  font-size: 16px;
}
.item-desc {
  margin: 0 0 5px 0;
  font-size: 12px;
  color: #666;
}
.item-price {
  margin: 0;
  font-size: 14px;
  color: #e74c3c;
}
.item-actions {
  display: flex;
  align-items: center;
}
.quantity {
  margin-right: 10px;
  font-size: 14px;
}
.remove-btn {
  background-color: transparent;
  border: none;
  color: #95a5a6;
  cursor: pointer;
  font-size: 14px;
}
.remove-btn:hover {
  color: #e74c3c;
}
.cart-footer {
  padding: 15px;
  border-top: 1px solid #e0e0e0;
}
.total {
  display: flex;
  justify-content: space-between;
  margin-bottom: 15px;
  font-size: 16px;
}
.total-price {
  font-weight: bold;
  color: #e74c3c;
}
.checkout-btn {
  width: 100%;
  padding: 10px;
  background-color: #2ecc71;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  font-size: 16px;
}
.checkout-btn:hover {
  background-color: #27ae60;
}
</style>
